jQuery(document).ready(function() {    
    // Datetime picker
    jQuery('#post-time').datetimepicker({
        dateFormat: 'dd/mm/yy',
        minDate: new Date()
    });

    jQuery("#timeline-form").validate({
        rules: {
            content: "required",
            address: "required"
        },
        messages: {
            content: "Thông tin cần nhập!",
            address: "Thông tin cần nhập!"
        }
    });
    
    jQuery('#cb-post-later').change(function() {
        if (jQuery(this).is(":checked")) {
            jQuery('#post-later').fadeIn();
        }
        else {
            jQuery('#post-later').fadeOut();
            jQuery('#post-time').val('');
        }
    });

    jQuery('#fileupload').fileupload({
        dataType: 'json',
        progressall: function(e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            jQuery('#progress-upload').fadeIn();
            jQuery('#progress-upload .bar .bluebar').css(
                    'width',
                    progress + '%'
                    );
        },
        done: function(e, data) {
            jQuery.each(data.result.files, function(index, file) {
                jQuery('#file_url').val('');
                if (file.url) {
                    jQuery('#file_wrapper').html('');
                    jQuery('#img-avatar').attr('src','');                    
                    jQuery('#file_url').val(file.url);
                    
                    //var audio_arr = ["audio/basic","audio/L24","audio/mp4","audio/mpeg","audio/mp3","audio/ogg","audio/vnd.wave"];
                    var image_arr = ["image/gif","image/jpeg","image/pjpeg","image/png","image/svg+xml","image/tiff"];
                    
                    if (jQuery.inArray(file.type,image_arr)>0) {
                        jQuery('#img-avatar').attr('src', file.url);
                    }
                    /*if (jQuery.inArray(file.type,audio_arr)>0) {
                        var content = '<audio controls>';
                        content += '<source src="' + file.url + '" type="audio/mpeg">';
                        content += '<source src="' + file.url + '" type="audio/ogg">';
                        content += '<source src="' + file.url + '" type="audio/mp3">';
                        content += 'Your browser does not support this audio format.';
                        content += '</audio>';
                        jQuery('#file_wrapper').html(content);
                    }*/
                }
            });
            jQuery('#progress-upload').fadeOut();
        }
    });
});